#!/usr/bin/env python
######################################################################
##
## Copyright (C) 2006,  Simon Kagstrom
##
## Filename:	  cibyl-generate-java-wrappers.py
## Author:		Simon Kagstrom <ska@bth.se>
## Description:   Generate Java and C syscall constants
##
## $Id: cibyl-generate-java-wrappers 14098 2007-03-10 07:50:32Z ska $
##
######################################################################
import sys, re, getopt
sys.path.append('%s/python/' % sys.path[0])
from Cibyl.SyscallHandling.syscallgenerator import *
from optparse import OptionParser

syscallDirectories = []
defines = []
headerDirectories = []
def includeCallback(option, opt, value, parser):
	syscallDirectories.append(value)

def defineCallback(option, opt, value, parser):
	defines.append("-D%s" % value)

def syscallCallback(option, opt, value, parser):
	headerDirectories.append(value)

usage = """Usage: cibyl-generate-java-wrappers [options] program set1 [set2 ...]

Generate Java wrappers from a compiled program, header directories and
generated system call directory. The directories header-dir are the
generated Cibyl system call headers. The directories 'syscall-dir'
contain the syscall sets 'set1', 'set2' etc. The sets are contained
within the directory 'dir', e.g., syscalls/j2me.
"""

parser = OptionParser(usage = usage)
parser.add_option("-o", "--outdir", dest="outdir", default='.',
				  help="Write output to outdir instead of current directory",
				  metavar="OUTDIR")
parser.add_option("-I", "--include", action="callback", help="""directory to search for header files.
You can pass multiple -I arguments.""",
				  type="str", nargs=1,
				  callback=includeCallback)
parser.add_option("-D", "--define", action="callback", help="""define a value for the preprocessor
(cpp). Used e.g., for jsr075 (FileConnection), which needs -D JSR075. You can pass multiple -D
arguments""",
		  type="str", nargs=1,
		  callback=defineCallback)
parser.add_option("-S", "--syscalls", action="callback", help="directory to search for system calls",
		  type="str", nargs=1,
		  callback=syscallCallback)
parser.add_option("-a", "--generate-all-syscalls", action="store_true", dest="generateAllSyscalls", default=False,
				  help="Generate all syscalls available. The ELF binary is then ignored")

(options, args) = parser.parse_args()

if not options.generateAllSyscalls and len(args) < 2:
	parser.error("incorrect number of arguments. Invoke with --help for arguments")
	sys.exit(1)

if options.generateAllSyscalls:
    elffile = None
    syscallSets = args
else:
    elffile = args[0]
    syscallSets = args[1:]

f = SyscallWrapperGenerator(elffile, syscallDirectories, headerDirectories, syscallSets,
			    outdir = options.outdir, defines = defines, generateAllSyscalls = options.generateAllSyscalls)
f.run()
